# ug2 Cleaning ------------------------------------------------------------

# This script implements cleaning by the IPA research assistant. Originally
# coded in Stata, it has been translated below into R.


# Reformatting date variables ---------------------------------------------

time_stamps <- names(ug2)[grepl(pattern = "\\btime_", x = names(ug2))]
bookends <- c("starttime", "endtime", "date_time")

for (variable in time_stamps) {
	ug2[variable] <-
		as.POSIXct(ug2[, variable], format = "%Y-%b-%d %I:%M:%S", tz = "Etc/GMT+3")
}
for (variable in bookends) {
	ug2[, variable] <-
		as.POSIXct(ug2[, variable], format = "%b %d, %Y %I:%M:%S %p", tz = "Etc/GMT+3")
}

rm(time_stamps, bookends, variable)

# Removing trial observations ---------------------------------------------

trials <- c(
	"uuid:491f1edc-69bc-4d43-9b53-5b2b9e179a50",
	"uuid:f2c36267-c8c7-4255-b984-ad6d3b3af29b",
	"uuid:3d3388dd-27f0-4146-896f-5611bdf11b6d",
	"uuid:f438f0a2-0e27-45d8-88b0-37dcccf9b023",
	"uuid:49743a8a-6b18-456c-8788-78dd0f578b13",
	"uuid:496860fb-e6dc-4c40-9f0a-b4f797e2798b",
	"uuid:4499291e-62f5-47d7-95c3-b25a6017ae72",
	"uuid:c6058aab-1d97-4dd2-b7d8-f3bcdcc88e30",
	"uuid:d630cffe-aef1-434f-a54c-02b5cb586e83",
	"uuid:321d6bc5-59a9-4ff2-8c97-0647c625cc52",
	"uuid:c22a129e-98fe-4f57-bc80-18d5a2b6f502",
	"uuid:6c790d4d-b9f7-4b5b-9189-40b284ac5993",
	"uuid:70221566-5a56-40fb-9044-5702e8e2a597",
	"uuid:f98ae107-0b9a-4aca-8e2a-c57406b18a32",
	"uuid:df97fdf8-4174-4ed6-bcbd-ecad33265dd4",
	"uuid:a3a5528e-c4d2-4e19-b3f2-7ad1b467b5db",
	"uuid:6ef5aaed-6ad6-4ae0-b4f8-577e0e40ca59",
	"uuid:0b32a63e-013c-483f-874a-70e1de6bcb71",
	"uuid:babbac3d-2eee-4390-a60d-b7db8a95eec2",
	"uuid:07ade1e9-a794-472b-890b-8ad084ae375b",
	"uuid:4b3412a4-71ba-4398-9958-759611ae45af",
	"uuid:5a00e801-9052-4caf-a275-f455e89c2135",
	"uuid:92f000cf-56f7-40e9-8e75-ef2bd834b119",
	"uuid:5cd8f810-7f1e-4225-9efe-9f08c2e10131",
	"uuid:e55f782d-58ed-41fd-bf8b-052582af1b1a",
	"uuid:7585f31c-0827-4e4f-b821-20816fe96809",
	"uuid:069b9c3b-0235-4adb-8919-2816e17075e9",
	"uuid:2d99ae1e-4b9c-4a95-9f7d-7898824310e8",
	"uuid:0e50dd1e-6c5e-4b1d-a2c6-27e7079c343e",
	"uuid:0d4a3839-d4eb-434a-8d63-7e2d9b60b51e",
	"uuid:d9724631-0841-434d-af63-7a681a8faef9",
	"uuid:f631f469-bc3d-4549-9711-e0bd03ae99cd",
	"uuid:cb17add9-5005-4459-b1d0-326ca494cee1",
	"uuid:7d4075af-e3e5-421e-9678-157071adc114",
	"uuid:a10411d5-e69a-4efd-90af-47a8e0e351c6",
	"uuid:121ed9ca-84ec-49ac-a953-07b4247bd3ad",
	"uuid:0e0e4e35-3599-44ba-af1c-e2810e0bffc7",
	"uuid:799ec207-1c6e-4286-aeee-970be894222b"
)

# Delete trials
ug2 <- ug2[-which(ug2$KEY %in% trials),]

rm(trials)

# Recoding IDs ------------------------------------------------------------

ug2$id[ug2$KEY == "uuid:30fb0480-6682-44a5-a7c7-52e39b573129"] <-
	40432121
ug2$id[ug2$KEY == "uuid:accd15a3-8584-4eec-85f0-a183e4bd0286"] <-
	40432411
ug2$id[ug2$KEY == "uuid:497d0372-1087-429e-8b48-894e270d4d75"] <-
	41047281
ug2$id[ug2$KEY == "uuid:7f0236ae-6247-4e1c-8fb4-23c5068c3e3f"] <-
	41047411
ug2$id[ug2$KEY == "uuid:986e7e79-77d7-4bab-9a6d-7a23ee4ce78a"] <-
	41020411
ug2$id[ug2$KEY == "uuid:4877718b-4d4b-49e1-b3dc-288efb1db07c"] <-
	41139511
ug2$id[ug2$KEY == "uuid:46e23605-7fa1-4059-a906-39185d94487f"] <-
	41139521
ug2$id[ug2$KEY == "uuid:6344771f-9407-4537-9544-6ca4d0acc19e"] <-
	41139531
ug2$id[ug2$KEY == "uuid:14a853df-7463-4eb2-a631-7012896ffc2c"] <-
	41139541
ug2$id[ug2$KEY == "uuid:bc118d1f-3141-4101-a0e9-d9e5551c2c51"] <-
	41139551
ug2$id[ug2$KEY == "uuid:3a221bbd-ca19-47b8-a433-e371fa73a3e0"] <-
	41139561
ug2$id[ug2$KEY == "uuid:b4fcff27-9059-40c7-ae08-4c519bfdc5f2"] <-
	41139571
ug2$id[ug2$KEY == "uuid:5d4ecd19-483e-44d1-83e0-3863c3c4dd8a"] <-
	41139581
ug2$id[ug2$KEY == "uuid:d4fd3b45-a1fd-4f76-8df9-940b8499f16c"] <-
	41139591
ug2$id[ug2$KEY == "uuid:184ce734-0a82-49c5-9a80-29fac62db076"] <-
	40427521
ug2$id[ug2$KEY == "uuid:c60be347-a534-464e-86f9-41fd08e310b2"] <-
	41115601
ug2$id[ug2$KEY == "uuid:3ff9435b-67b0-4cc8-a4d8-5de680a2757a"] <-
	41430291
ug2$id[ug2$KEY == "uuid:ae57fb09-8992-4547-9b75-7873f9ce2ab3"] <-
	40207511
ug2$id[ug2$KEY == "uuid:1aa58c60-baf3-46e5-b356-6e515fca9048"] <-
	40216311
ug2$id[ug2$KEY == "uuid:66c48d8a-4b91-4b6e-933f-359fed675aa5"] <-
	40216251
ug2$id[ug2$KEY == "uuid:3f8786c4-a2c3-4e05-9263-d961700f72c1"] <-
	41115901
ug2$id[ug2$KEY == "uuid:91d46b02-5849-43da-a692-0f228f654690"] <-
	41115921
ug2$id[ug2$KEY == "uuid:313ebfc7-83c9-4f56-bf5d-ce39728daa64"] <-
	40621261
ug2$id[ug2$KEY == "uuid:cbda9854-379e-46ef-b861-6fc3971003fa"] <-
	40621311
ug2$id[ug2$KEY == "uuid:8d61e70e-4571-4774-9f08-6a04e33f2d5e"] <-
	41139601
ug2$id[ug2$KEY == "uuid:1d52f74f-327c-4d24-a8e7-43d843f717eb"] <-
	41714351
ug2$id[ug2$KEY == "uuid:6b3f7427-fa37-430d-a148-2e4495b6e4de"] <-
	40926111
ug2$id[ug2$KEY == "uuid:586e5bbc-ddec-4170-9dee-210291ef9b34"] <-
	41006501
ug2$id[ug2$KEY == "uuid:9d945b61-f0ac-47c4-a735-1a9f756350fa"] <-
	41601481
ug2$id[ug2$KEY == "uuid:c32fe6d5-b177-4f1f-ada9-bfb5c486f509"] <-
	41601061
ug2$id[ug2$KEY == "uuid:6b3f7427-fa37-430d-a148-2e4495b6e4de"] <-
	40926111
ug2$id[ug2$KEY == "uuid:e899a473-65c2-4096-b73d-3e67aee169a3"] <-
	41115931
ug2$id[ug2$KEY == "uuid:ca78b892-5927-4dc6-a094-18f257343545"] <-
	41610611
ug2$id[ug2$KEY == "uuid:c8616dd0-d76f-41c9-8696-e66303bb9181"] <-
	41714421
ug2$id[ug2$KEY == "uuid:2c9c3999-671c-4073-aab4-5996e8a59c7e"] <-
	40346261
ug2$id[ug2$KEY == "uuid:825de3d6-deb4-4b23-9359-560034ed75f3"] <-
	40329181
ug2$id[ug2$KEY == "uuid:adef0a1d-69c2-43df-bcbe-048421f1b70f"] <-
	40329311
ug2$id[ug2$KEY == "uuid:636b5221-dc2f-43b2-b720-716f28657c43"] <-
	40345611
ug2$id[ug2$KEY == "uuid:cdcde092-fa4b-4c5a-9b29-53d4591c182f"] <-
	41610621
ug2$id[ug2$KEY == "uuid:c2cdd49c-a209-494d-944e-1d4d1c253fbd"] <-
	41610381
ug2$id[ug2$KEY == "uuid:2cbc1fbe-4ac6-4911-89ae-6461febdbd00"] <-
	41240511
ug2$id[ug2$KEY == "uuid:f006171c-521d-4856-bce1-5c1dbd6bb091"] <-
	41219481
ug2$id[ug2$KEY == "uuid:bd5f5f51-5f11-4404-a9ff-10a0a827a9c5"] <-
	41522511
ug2$id[ug2$KEY == "uuid:2c15ceb4-4b26-4f10-9b62-9d6f2b8e5485"] <-
	40345461
ug2$id[ug2$KEY == "uuid:a0aecb3c-1e74-450d-8afc-7ae1bca73dd2"] <-
	40135191
ug2$id[ug2$KEY == "uuid:64224ee8-d42e-4555-b351-5f8b4340d970"] <-
	40135041
ug2$id[ug2$KEY == "uuid:6d95dbc6-5cc8-4ebc-b155-9f39a400aed2"] <-
	40328261
ug2$id[ug2$KEY == "uuid:560922b9-1fa9-4847-999c-a2d74943b32e"] <-
	40328511
ug2$id[ug2$KEY == "uuid:3ae90603-6bac-4e5d-b94c-16e8893e2ad9"] <-
	30624331
ug2$id[ug2$KEY == "uuid:51928401-7177-4560-9f4f-bff535d0996d"] <-
	30624151
ug2$id[ug2$KEY == "uuid:3cae54af-eb6b-4745-b3c6-98a695dfa6cb"] <-
	31102281
ug2$id[ug2$KEY == "uuid:bfc543fb-072c-459b-b9e2-e61e71f59a7f"] <-
	31025481
ug2$id[ug2$KEY == "uuid:553f2091-d976-49d8-9831-fad825e29827"] <-
	30709061
ug2$id[ug2$KEY == "uuid:0db16bf3-837c-40a3-9b6f-a8ae19a6e1ae"] <-
	30606061
ug2$id[ug2$KEY == "uuid:27027d6f-f2a6-42ca-9748-321fe7b50b30"] <-
	30203481
ug2$id[ug2$KEY == "uuid:5d641683-e7f8-4942-85c7-18a69ad0eefc"] <-
	30203041
ug2$id[ug2$KEY == "uuid:5b955afa-905e-4390-8516-663f73a87cc3"] <-
	30712061
ug2$id[ug2$KEY == "uuid:311c2c2e-2dee-4392-b989-da3d01c68adf"] <-
	31132231
ug2$id[ug2$KEY == "uuid:c6de38b4-2fd6-4eb1-825a-b6dd84d26177"] <-
	31132011
ug2$id[ug2$KEY == "uuid:5328e531-7bdb-482b-b182-e177ca88103a"] <-
	31025301
ug2$id[ug2$KEY == "uuid:5908f375-8b4f-42c2-8bcd-3829916f77f3"] <-
	41709491
ug2$id[ug2$KEY == "uuid:28631fe2-cb42-4c1b-876e-3b072eb20d6c"] <-
	41709501
ug2$id[ug2$KEY == "uuid:7faea7ba-f2d6-42d6-86ba-e45c5eb46b26"] <-
	30818061
ug2$id[ug2$KEY == "uuid:880a816f-aaf9-4e11-9e6e-b2a30946e122"] <-
	30408191
ug2$id[ug2$KEY == "uuid:32cb3d24-b11d-4cec-87e1-83231e278944"] <-
	30529211
ug2$id[ug2$KEY == "uuid:6b7ba34f-4674-4ee5-88dc-c0926930e84d"] <-
	30317231
ug2$id[ug2$KEY == "uuid:ce863eb0-598c-46da-84c0-4e721fed1040"] <-
	30317171
ug2$id[ug2$KEY == "uuid:2dbe24ce-18ee-4700-9a36-16fd9f6399d1"] <-
	40908421
ug2$id[ug2$KEY == "uuid:92f82100-a115-4a38-954e-b6d1d53a28a8"] <-
	40534201
ug2$id[ug2$KEY == "uuid:46c9d1f9-b1df-4a76-93a0-453c3f468d25"] <-
	10312481
ug2$id[ug2$KEY == "uuid:a603fce3-8dae-41c8-9967-a111eedea944"] <-
	31028491
ug2$id[ug2$KEY == "uuid:f145b17d-cf22-43b5-833c-5cf3d0855a4c"] <-
	10308531
ug2$id[ug2$KEY == "uuid:2a36d515-e40e-4965-a414-cc55490106a4"] <-
	10214451
ug2$id[ug2$KEY == "uuid:e824bb61-2d48-4438-b8e1-9d54015d4494"] <-
	10713281
ug2$id[ug2$KEY == "uuid:c9e3d3bb-dfef-433c-8831-54390a97a579"] <-
	10710281
ug2$id[ug2$KEY == "uuid:a5f18e79-ee81-4932-97af-7940a1379f72"] <-
	20516331
ug2$id[ug2$KEY == "uuid:889224a1-ba6b-4e97-a3e4-fcd158e2fa85"] <-
	20516131
ug2$id[ug2$KEY == "uuid:a2e915da-acb9-4050-8eac-05b1593a29dd"] <-
	20505201
ug2$id[ug2$KEY == "uuid:aed6db20-2e7b-438c-8672-8fce6f7b848f"] <-
	20509391
ug2$id[ug2$KEY == "uuid:bb98d500-623a-436a-9716-84d71d404edf"] <-
	10402421
ug2$id[ug2$KEY == "uuid:d4da6618-689c-4c69-be8b-ea219de2fa91"] <-
	10214341
ug2$id[ug2$KEY == "uuid:b03d7641-e99a-4141-8af8-95690ce58f0d"] <-
	20403091
ug2$id[ug2$KEY == "uuid:fdffcca5-f903-4211-8314-1a7a946c1ed6"] <-
	20311441
ug2$id[ug2$KEY == "uuid:ad216561-45e7-4e60-a4a5-5fd3887877d6"] <-
	20311171
ug2$id[ug2$KEY == "uuid:55bdec99-f8b3-44d3-803e-bebf2db62273"] <-
	20212281
ug2$id[ug2$KEY == "uuid:7b105d43-7db5-422a-9a0c-8fcae2a3f898"] <-
	20212391
ug2$id[ug2$KEY == "uuid:26d87e33-1d06-40f8-810e-b550ec87bdbf"] <-
	20212411
ug2$id[ug2$KEY == "uuid:36fd1f35-1cf1-4f69-a4f9-60c8cc38af1b"] <-
	20413331
ug2$id[ug2$KEY == "uuid:60361280-6859-4b4b-b384-1996121efc7c"] <-
	20413141
ug2$id[ug2$KEY == "uuid:5fad8f69-9133-449c-809c-6d4e7f0a8af3"] <-
	20601181
ug2$id[ug2$KEY == "uuid:b55335ad-be4a-491f-ba66-140ac794078a"] <-
	40825911
ug2$id[ug2$KEY == "uuid:eda17908-e175-4e6f-a78b-15d29438ca3c"] <-
	41344931
ug2$id[ug2$KEY == "uuid:f850728b-1f4c-47d2-9946-837aa3968131"] <-
	30408891

# Recoding ‘other’ responses ----------------------------------------------

ug2cb$rel_q2_4
ug2$rel_q2_4[grepl(pattern = "Ali nakyeyombekedde|She's single|She single|She is single|single",
									x = ug2$q2_4_oth,
									ignore.case = TRUE)] <- 1

ug2$rel_q2_4[grepl(pattern = "School Dormitory",
									x = ug2$q2_4_oth,
									ignore.case = TRUE)] <- 1

ug2cb$status_q2_5
ug2$status_q2_5[grepl(pattern = "widow",
										 x = ug2$q2_5_oth,
										 ignore.case = TRUE)] <- 6


ug2$lang_q2_23[ug2$key == "uuid:2d1d7c2c-e167-4462-aa9a-66813cf565c5" |
								ug2$key == "uuid:5de168bc-22fd-48e0-a501-acb5302b7c0d"] <- 26
ug2$lang_q2_23[ug2$key == "uuid:c351a962-359a-446b-88e9-a643f19342df"] <-
	3

ug2cb$other_lang_q2_24
ug2$other_lang_q2_24_4[ug2$q2_24_oth == "Lwamba"] <- 24

ug2$other_lang_q2_24_4[grepl(pattern = "Kakuua",
														x = ug2$q2_24_oth,
														ignore.case = TRUE)] <- 9
ug2$other_lang_q2_24_4[grepl(pattern = "Lululu|Luruuru",
														x = ug2$q2_24_oth,
														ignore.case = TRUE)] <- 28

ug2cb$tribe_q2_25
ug2$tribe_q2_25[grepl(pattern = "Mulundi|Murundi", x = ug2$q2_25_oth)] <-
	34
ug2$tribe_q2_25[grepl(pattern = "Mukonjo|Bakonjo|Bakonjjo", x = ug2$q2_25_oth)] <-
	17
ug2$tribe_q2_25[grepl(pattern = "Mukakwa|Kakuua|Mukakwa", x = ug2$q2_25_oth)] <-
	8
ug2$tribe_q2_25[grepl(pattern = "Mululu|Muluulu|Muruuru", x = ug2$q2_25_oth)] <-
	20
ug2$tribe_q2_25[grepl(pattern = "Mulalo", x = ug2$q2_25_oth)] <- 21
ug2$tribe_q2_25[grepl(pattern = "Mukochi", x = ug2$q2_25_oth)] <- 1

ug2cb$activity_q3_1
ug2$activity_q3_1[grepl(pattern = "Charcoal burning",
											 x = ug2$q3_1_oth,
											 ignore.case = TRUE)] <- 4
ug2$activity_q3_1[grepl(pattern = "Roasting meat|Drugshop|Selling clothes|Trading in crops|Trader|Drug shop|assistant working in a drug shop|Selling  cassavaq|Phone accesory|Selling electricals|Selling clothes in a boutique|Businessman in onions|Mutembeyi|Kusiika chapati|Produce trader|Boutique|Drug Shop keeper|Drug shop owner|Butchery|Butcher man|He is a butcher",
											 x = ug2$q3_1_oth,
											 ignore.case = TRUE)] <- 7
ug2$activity_q3_1[grepl(pattern = "Tailor|Sewing",
											 x = ug2$q3_1_oth,
											 ignore.case = TRUE)] <- 11
ug2$activity_q3_1[grepl(pattern = "Boda|Rider|driver",
											 x = ug2$q3_1_oth,
											 ignore.case = TRUE)] <- 13
ug2$activity_q3_1[grepl(pattern = "Teacher|Teaching",
											 x = ug2$q3_1_oth,
											 ignore.case = TRUE)] <- 16

ug2$activity_q3_1[grepl(pattern = "Hair Stylist|Hair dresser|Working in my salon|Hair cutting",
											 x = ug2$q3_1_oth,
											 ignore.case = TRUE)] <- 7
ug2$activity_q3_1[grepl(pattern = "saloon|Bar attendant|Selling drink|\\bbar\\b",
											 x = ug2$q3_1_oth,
											 ignore.case = TRUE)] <- 9
ug2$activity_q3_1[grepl(pattern = "Produce buying|Selling  cassava|Dealing in gold selling|Chapati making|Kusubula  bbirime|Making pancakes|Okutunda ebyokunywa|Produce trading|working in a drug shop|Music cutting and video library|Mobile money|Petrol station|Drug shop assistant",
											 x = ug2$q3_1_oth,
											 ignore.case = TRUE)] <- 7
ug2$activity_q3_1[grepl(pattern = "Musomesaw abantu bakulu|Teacher(Agriculture)",
											 x = ug2$q3_1_oth,
											 ignore.case = TRUE)] <- 16
ug2$activity_q3_1[grepl(pattern = "Muvubi|Fisherman",
											 x = ug2$q3_1_oth,
											 ignore.case = TRUE)] <- 19
ug2$activity_q3_1[grepl(pattern = "Repairing boats|Shoe repairer",
											 x = ug2$q3_1_oth,
											 ignore.case = TRUE)] <- 10
ug2$activity_q3_1[grepl(pattern = "Buider|builder",
											 x = ug2$q3_1_oth,
											 ignore.case = )] <- 6

ug2cb$religion_q14_1
ug2$religion_q14_1[grepl(
	pattern = "No religion|Has no religion but believes in God",
	x = ug2$religion_q14_1_ot,
	ignore.case = TRUE
)] <- 1
ug2$religion_q14_1[grepl(
	pattern = "Christian for Mungu",
	x = ug2$religion_q14_1_ot,
	ignore.case = TRUE
)] <- 9

ug2$dwelling_q16_1[grepl(pattern = "Rental|Muzigo  ,rental|muzigo|Alin ku Muzigo|Apangisa muzigo|Entomba yakumizigo",
												x = ug2$q16_1_oth,
												ignore.case = TRUE)] <- 2

ug2cb$report_q16_1c
# This case was originally coded as none, but they respond they reported it to LC1:
ug2$report_q16_1c[grepl(pattern = "2 5",
											 x = ug2$report_q16_1c,
											 ignore.case = TRUE)] <- 2
ug2$report_q16_1c[grepl(
	pattern = "Tewuliranga ko omuntu yena|No",
	x = ug2$report_q16_1c_oth,
	ignore.case = TRUE
)] <- 4

ug2cb$content_q17_1b
ug2$content_q17_1b[grepl(
	pattern = "Recalls movies not treatments",
	x = ug2$content_q17_1b_oth,
	ignore.case = TRUE
)] <- 5
ug2$content_q17_1b_1[grepl(
	pattern = "Other project not social norms|She attended a different movieHe only watched one movie and found it ending. They dint remember anything else|Didn't get to know",
	x = ug2$content_q17_1b_oth,
	ignore.case = TRUE
)] <- 4

ug2cb$hear_q19_4
ug2$hear_q19_4_1[grepl(
	pattern = "Didn't mind to ask|Didn't ask any body|Didn't hear anything about them|They didn't tell me|He only recalls about the lady that used to screen the movies|Don't know|Didn't bother|Didn't get time to ask them|He didn't ask about the content|They didn't discuss anything|999|Doesn't know|Was not told anything about the movies|Its only the husband who went and he didn't tell her anything about the movie|He did not watch any movie|The respondent  said that she didn't take an initiative to ask them what was in the movies|I did not ask them anything|Does not know of anything|Does not know any content|None|She wasnt told in details about the movie by the husband|Didn't hear anything about the content of the films|She doesn't know anything about them.|He didn't discuss with them anything, so didn't know about the content.|Doesn't know way we're they about|othing he heard  or knows|None|They didn't tell him anything|Didn't get time to ask them|She didn't hear of anything.|They didn't discuss anything with him.|He doesn't know and dint ask at the movies|Didn't bother|He didn't watch any movie|They didn't talk about them|Nothing he heard  or knows",
	x = ug2$hear_q19_4_oth,
	ignore.case = TRUE
)] <- 7
ug2$hear_q19_4_1[grepl(
	pattern = "She had about them but she cannot recall any thing",
	x = ug2$hear_q19_4_oth,
	ignore.case = TRUE
)] <- 4


# Recoding area variables -------------------------------------------------

ug2$county[grepl(pattern = "uuid:3eb540db-2685-4e15-8ee2-27d84364a475|uuid:5a3b1fb5-6335-46e1-b8a7-8804bc6fe134",
								x = ug2$KEY,
								ignore.case = TRUE)] <- 5
ug2$subcounty[grepl(pattern = "uuid:3eb540db-2685-4e15-8ee2-27d84364a475|uuid:5a3b1fb5-6335-46e1-b8a7-8804bc6fe134",
									 x = ug2$KEY,
									 ignore.case = TRUE)] <- 27
ug2$parish[grepl(pattern = "uuid:3eb540db-2685-4e15-8ee2-27d84364a475|uuid:5a3b1fb5-6335-46e1-b8a7-8804bc6fe134",
								x = ug2$KEY,
								ignore.case = TRUE)] <- 68
ug2$tc_id[grepl(pattern = "uuid:3eb540db-2685-4e15-8ee2-27d84364a475|uuid:5a3b1fb5-6335-46e1-b8a7-8804bc6fe134",
							 x = ug2$KEY,
							 ignore.case = TRUE)] <- 74
ug2$county[grepl(pattern = "uuid:02e96a04-80ba-4c3b-be34-e1713a0030f5",
								x = ug2$KEY,
								ignore.case = TRUE)] <- 7
ug2$subcounty[grepl(pattern = "uuid:02e96a04-80ba-4c3b-be34-e1713a0030f5",
									 x = ug2$KEY,
									 ignore.case = TRUE)] <- 36
ug2$parish[grepl(pattern = "uuid:02e96a04-80ba-4c3b-be34-e1713a0030f5",
								x = ug2$KEY,
								ignore.case = TRUE)] <- 90
ug2$tc_id[grepl(pattern = "uuid:02e96a04-80ba-4c3b-be34-e1713a0030f5",
							 x = ug2$KEY,
							 ignore.case = TRUE)] <- 100
ug2$parish[grepl(pattern = "uuid:34542561-ad9e-419e-9179-d9cf164931c0|uuid:0e786b45-0f67-4e8d-90f8-9e922e617d52|uuid:bfd53835-8427-4f0d-a63f-0a82bf7a9d19|uuid:014d4253-053b-469d-ae66-5f2469596398|uuid:3d1a1f04-5fd1-4910-9664-ac093e88442a|uuid:7369ba79-fded-4268-95a2-4b5cb9c75dda|uuid:5211dd3b-bb0e-46ab-ae80-071b725c9bb9|uuid:0b7ded7a-5300-4728-8a57-7ee9c4da502b|uuid:9651a1c4-2fb1-484c-a535-69763b8c13c8|uuid:04aa1766-cdca-4b1c-97c7-9660ca087abd|uuid:853dcfd3-7be5-46a0-a134-c56217ad0e2b|uuid:e310cc42-b340-42b2-93f9-8d9088782acc|uuid:3fc6c51e-31d2-4c3f-8b57-85849a572748|uuid:fb6da6ab-d0f1-47b5-b632-e59652a467d5|uuid:9b4c2395-c65d-4e2c-9ee4-80adc366cb93|uuid:bc92c2a0-4363-4707-a260-d657137be01b|uuid:0bf2d665-64d3-4c2d-b60a-187627f9224b|uuid:9779c5d6-7025-4aff-88a0-5a8712147926|uuid:644c7a00-e8f5-4201-bd08-451ca7d1cdbd",
								x = ug2$KEY,
								ignore.case = TRUE)] <- 69
ug2$tc_id[grepl(pattern = "uuid:34542561-ad9e-419e-9179-d9cf164931c0|uuid:0e786b45-0f67-4e8d-90f8-9e922e617d52|uuid:bfd53835-8427-4f0d-a63f-0a82bf7a9d19|uuid:014d4253-053b-469d-ae66-5f2469596398|uuid:3d1a1f04-5fd1-4910-9664-ac093e88442a|uuid:7369ba79-fded-4268-95a2-4b5cb9c75dda|uuid:5211dd3b-bb0e-46ab-ae80-071b725c9bb9|uuid:0b7ded7a-5300-4728-8a57-7ee9c4da502b|uuid:9651a1c4-2fb1-484c-a535-69763b8c13c8|uuid:04aa1766-cdca-4b1c-97c7-9660ca087abd|uuid:853dcfd3-7be5-46a0-a134-c56217ad0e2b|uuid:e310cc42-b340-42b2-93f9-8d9088782acc|uuid:3fc6c51e-31d2-4c3f-8b57-85849a572748|uuid:fb6da6ab-d0f1-47b5-b632-e59652a467d5|uuid:9b4c2395-c65d-4e2c-9ee4-80adc366cb93|uuid:bc92c2a0-4363-4707-a260-d657137be01b|uuid:0bf2d665-64d3-4c2d-b60a-187627f9224b|uuid:9779c5d6-7025-4aff-88a0-5a8712147926|uuid:644c7a00-e8f5-4201-bd08-451ca7d1cdbd",
							 x = ug2$KEY,
							 ignore.case = TRUE)] <- 75
ug2$tc_id[grepl(pattern = "uuid:0bf2d665-64d3-4c2d-b60a-187627f9224b|uuid:9b4c2395-c65d-4e2c-9ee4-80adc366cb93|uuid:bc92c2a0-4363-4707-a260-d657137be01b|uuid:644c7a00-e8f5-4201-bd08-451ca7d1cdbd|uuid:9779c5d6-7025-4aff-88a0-5a8712147926",
							 x = ug2$KEY,
							 ignore.case = TRUE)] <- 76
ug2$parish[grepl(pattern = "uuid:0f0cfbd0-70e2-433d-9596-8fa45bff7847",
								x = ug2$KEY,
								ignore.case = TRUE)] <- 99
ug2$tc_id[grepl(pattern = "uuid:0f0cfbd0-70e2-433d-9596-8fa45bff7847",
							 x = ug2$KEY,
							 ignore.case = TRUE)] <- 109
ug2$tc_id[grepl(pattern = "uuid:ab45c2a0-2987-45c2-a0a6-801ace86d03e",
							 x = ug2$KEY,
							 ignore.case = TRUE)] <- 98
ug2$tc_id[grepl(pattern = "uuid:f38ca3cd-7c43-4c18-b1ff-0b69b8ceb86f" ,
							 x = ug2$KEY,
							 ignore.case = TRUE)] <- 59
ug2$parish[grepl(pattern = "uuid:f38ca3cd-7c43-4c18-b1ff-0b69b8ceb86f",
								x = ug2$KEY,
								ignore.case = TRUE)] <- 55
ug2$tc_id[grepl(pattern = "uuid:3f41ea6c-581e-4970-9901-106509ca06e2",
							 x = ug2$KEY,
							 ignore.case = TRUE)] <- 56
ug2$parish[grepl(pattern = "uuid:3f41ea6c-581e-4970-9901-106509ca06e2",
								x = ug2$KEY,
								ignore.case = TRUE)] <- 52
ug2$county[grepl(pattern = "uuid:a01e9aca-fae2-421a-951f-c9af92f2f5b7",
								x = ug2$KEY,
								ignore.case = TRUE)] <- 5
ug2$subcounty[grepl(pattern = "uuid:a01e9aca-fae2-421a-951f-c9af92f2f5b7",
									 x = ug2$KEY,
									 ignore.case = TRUE)] <- 26
ug2$parish[grepl(pattern = "uuid:a01e9aca-fae2-421a-951f-c9af92f2f5b7",
								x = ug2$KEY,
								ignore.case = TRUE)] <- 65
ug2$tc_id[grepl(pattern = "uuid:a01e9aca-fae2-421a-951f-c9af92f2f5b7",
							 x = ug2$KEY,
							 ignore.case = TRUE)] <- 70
ug2$tc_id[grepl(pattern = "uuid:eb5cf4fc-e391-4feb-9113-e367f3956945",
							 x = ug2$KEY,
							 ignore.case = TRUE)] <- 59
ug2$parish[grepl(pattern = "uuid:eb5cf4fc-e391-4feb-9113-e367f3956945",
								x = ug2$KEY,
								ignore.case = TRUE)] <- 55

ids_of_kibuye_village <- c(
	"10707121",
	"10707341",
	"10707411",
	"10707161",
	"10707351",
	"10707361",
	"10707191",
	"10707441",
	"10707301",
	"10707311",
	"10707061",
	"10707491",
	"10707321",
	"10707261",
	"10707141",
	"10707111",
	"10707071",
	"10707021",
	"10707431",
	"10707231",
	"10707181",
	"10707031",
	"10707241",
	"10707091",
	"10707171",
	"10707151",
	"10707451",
	"10707391",
	"10707251",
	"10707331",
	"10707401",
	"10707271",
	"10707291",
	"10707221",
	"10707201",
	"10707471",
	"10707481",
	"10707461",
	"10707371",
	"10707211",
	"10707381",
	"10707281",
	"10707081",
	"10707041",
	"10707011",
	"10707421",
	"10707131",
	"10707101",
	"10707051"
)

ug2$tc_id[ug2$id %in% ids_of_kibuye_village] <- 12
ug2$tc_id[grepl(pattern = "uuid:1eb6d3c9-7cbb-4b61-a2ff-1bbdeab10eb8",
							 x = ug2$KEY,
							 ignore.case = TRUE)] <- 12
ug2$tc_id[grepl(pattern = "uuid:2a73d6d0-3a1d-4987-9491-28ecb510a6a6|uuid:5541da77-3917-457d-a9ef-4ce02834b510|uuid:63b863b9-3230-4d63-9cd2-f7a711f97d60|uuid:a62f6ac8-063c-4417-a2b5-924706b6376a|uuid:b363213a-3423-454e-accf-682c34526a0f|uuid:84bf564d-b121-4baf-ad91-96e8baa13096",
							 x = ug2$KEY,
							 ignore.case = TRUE)] <- 28
ug2$parish[grepl(pattern = "uuid:2a73d6d0-3a1d-4987-9491-28ecb510a6a6|uuid:5541da77-3917-457d-a9ef-4ce02834b510|uuid:63b863b9-3230-4d63-9cd2-f7a711f97d60|uuid:a62f6ac8-063c-4417-a2b5-924706b6376a|uuid:b363213a-3423-454e-accf-682c34526a0f|uuid:84bf564d-b121-4baf-ad91-96e8baa13096",
								x = ug2$KEY,
								ignore.case = TRUE)] <- 24
ug2$tc_id[grepl(pattern = "uuid:aed6db20-2e7b-438c-8672-8fce6f7b848f",
							 x = ug2$KEY,
							 ignore.case = TRUE)] <- 26
ug2$tc_id[grepl(pattern = "uuid:d6baf787-8352-4164-bb95-66eda21891ba|uuid:001cccdd-76f3-4a50-8add-88785e8b8bcb|uuid:fd1959ff-d026-4d02-a24c-0be5c7768073|uuid:73456bbe-59d6-4c83-ae53-a84013b39b26|uuid:d12dbe86-1b44-4d88-843c-b80989ba7623|uuid:eaba4d81-0736-4a09-806e-6418c31a39af",
							 x = ug2$KEY,
							 ignore.case = TRUE)] <- 26
ug2$tc_id[grepl(pattern = "uuid:dc0e5182-f7dd-4ae0-b666-605b1f357583|uuid:9dc9efd5-ed26-49be-ac6e-dce12218a6d1",
							 x = ug2$KEY,
							 ignore.case = TRUE)] <- 6
ug2$parish[grepl(pattern = "uuid:da1bc67c-f555-4cb4-8f66-f86409d12347",
								x = ug2$KEY,
								ignore.case = TRUE)] <- 12
ug2$tc_id[grepl(pattern = "uuid:da1bc67c-f555-4cb4-8f66-f86409d12347",
							 x = ug2$KEY,
							 ignore.case = TRUE)] <- 16

# Correction of tracking variable
ug2cb$tracking
ug2$tracking[ug2$KEY == "uuid:1f3a38f1-f9e3-4996-9e1c-0d3fc8ca16b4" |
							ug2$KEY == "uuid:b16d693a-cdea-4951-8ced-12756432c29e"] <- 1
ug2$tracking[ug2$enum != 11 &
							ug2$enum != 41 & ug2$enum != 13 &
							ug2$enum != 3 & ug2$enum != 36 & ug2$enum != 37] <- 1

# GPS coordinates not recorded
ug2$gps_longitude[grepl(pattern = "uuid:3e95d433-bb29-408f-ba35-8540359897df|uuid:42644443-2844-4032-b910-af17b544bb1f|uuid:9aff975a-6a88-4752-8f43-7825b01b231e|uuid:cb4b2109-c6df-450d-87fe-4398493e383f",
											 x = ug2$KEY,
											 ignore.case = TRUE)] <- NA
ug2$gps_latitude[grepl(pattern = "uuid:3e95d433-bb29-408f-ba35-8540359897df|uuid:42644443-2844-4032-b910-af17b544bb1f|uuid:9aff975a-6a88-4752-8f43-7825b01b231e|uuid:cb4b2109-c6df-450d-87fe-4398493e383f",
											x = ug2$KEY,
											ignore.case = TRUE)] <- NA

# Enumerators selecting wrong names
ug2$enum[grepl(pattern = "uuid:42481a49-e1dc-45bc-98f2-d530894daafb|uuid:d73cffd8-d3df-4771-9b9c-d3aad41aa793|uuid:19ca0525-7fc0-4949-bf71-ab379fc9864b",
							x = ug2$KEY,
							ignore.case = TRUE)] <- 1
ug2$enum[grepl(pattern = "uuid:07ab4640-f16b-4a09-89bc-acdff7f80389|uuid:dd28fcdc-9e60-4ad2-98d7-577b0d7bd6d4",
							x = ug2$KEY,
							ignore.case = TRUE)] <- 6
ug2$enum[grepl(pattern = "uuid:e32c5c65-c903-4775-8007-c7618e93f30b",
							x = ug2$KEY,
							ignore.case = TRUE)] <- 21
ug2$enum[grepl(pattern = "uuid:67877f6c-6d1d-4450-8790-a9fa01e43db3",
							x = ug2$KEY,
							ignore.case = TRUE)] <- 12
ug2$enum[grepl(pattern = "uuid:d82dcd4b-5823-494a-a07e-0db62939e5b3",
							x = ug2$KEY,
							ignore.case = TRUE)] <- 21
ug2$enum[grepl(pattern = "uuid:5e8a5c30-0e10-4043-943a-33a0bfb1ad3f",
							x = ug2$KEY,
							ignore.case = TRUE)] <- 29
ug2$enum[grepl(pattern = "uuid:b4c0fe9f-cd03-4823-8125-26e5bf858b6e",
							x = ug2$KEY,
							ignore.case = TRUE)] <- 36
ug2$enum[grepl(pattern = "uuid:000e0311-20d5-4e2f-ad60-a46497acc9af",
							x = ug2$KEY,
							ignore.case = TRUE)] <- 37

# Enumerators entering wrong sex
ug2$sex_q2_1[grepl(pattern = "uuid:e9152aab-b4f6-4514-afc5-a8f94199adb9",
									x = ug2$KEY,
									ignore.case = TRUE)] <- 2
ug2$sex_q2_1[grepl(pattern = "uuid:31d0389e-1b1c-4077-99ab-5246a49a75f0",
									x = ug2$KEY,
									ignore.case = TRUE)] <- 1
ug2$sex_q2_1[grepl(pattern = "uuid:c7851174-5fb1-46ae-b1f0-9f49da74cc14",
									x = ug2$KEY,
									ignore.case = TRUE)] <- 1


# Conative Attitudes ------------------------------------------------------

# Function for Aggregating Behavioral Proclivities 

get_dummy_ug2 <- function(new_var, old_vars, pos_level, neg_level) {
	for(i in 1:length(old_vars)) {
		# uses <<- to perform assignment in global environment:
		ug2[,new_var][ug2[,old_vars[i]] == pos_level] <<- 1
		ug2[,new_var][ug2[,old_vars[i]] == neg_level] <<- 0
	}
	return(data)
}


# Conative Attitudes ------------------------------------------------------

# IPV ---------------------------------------------------------------------

# The action in line with the norm is 1 for the first q,
# 2 for the second, 1 for the third, and 2 for the fourth
ug2cb$action_q8_1a_1$answer[1,]
ug2cb$action_q8_1a_2$answer[1,]
ug2cb$action_q8_1a_3$answer[1,]
ug2cb$action_q8_1a_4$answer[1,]
ug2cb$action_q8_1b_1$answer[2,]
ug2cb$action_q8_1b_2$answer[2,]
ug2cb$action_q8_1b_3$answer[2,]
ug2cb$action_q8_1b_4$answer[2,]
ug2cb$action_q8_1c_1$answer[1,]
ug2cb$action_q8_1c_2$answer[1,]
ug2cb$action_q8_1c_3$answer[1,]
ug2cb$action_q8_1c_4$answer[1,]
ug2cb$action_q8_1d_1$answer[2,]
ug2cb$action_q8_1d_2$answer[2,]
ug2cb$action_q8_1d_3$answer[2,]
ug2cb$action_q8_1d_4$answer[2,]

# Create blank variables
ipv_vars <- c("report_police",
							"involve_lc1",
							"involve_parents",
							"involve_nabakyala") 
ug2[,ipv_vars] <- NA

# mapply function through parameters
mapply(
	FUN = get_dummy_ug2,
	new_var = ipv_vars,
	old_vars = list(c("action_q8_1a_1",
										"action_q8_1a_2",
										"action_q8_1a_3",
										"action_q8_1a_4"),
									c("action_q8_1b_1",
										"action_q8_1b_2",
										"action_q8_1b_3",
										"action_q8_1b_4"),
									c("action_q8_1c_1",
										"action_q8_1c_2",
										"action_q8_1c_3",
										"action_q8_1c_4"),
									c("action_q8_1d_1",
										"action_q8_1d_2",
										"action_q8_1d_3",
										"action_q8_1d_4")),
	pos_level = c(1,2,1,2),
	neg_level = c(2,1,2,1))

rm(ipv_vars)

# Additional variables for repsondents who are lc1s
lc1s <- c("action_q8_1b_2_1",
					"action_q8_1b_2_2",
					"action_q8_1b_2_3",
					"action_q8_1b_2_4")

# Answer 2 is in line with the norms for LC1s
ug2cb$action_q8_1b_2_2$answer[2,]
ug2cb$action_q8_1b_2_2$answer[2,]
ug2cb$action_q8_1b_2_3$answer[2,]
ug2cb$action_q8_1b_2_4$answer[2,]

for(variable in lc1s) {
	ug2$involve_lc1[ug2[,variable] == 2] <- 1
	ug2$involve_lc1[ug2[,variable] == 1] <- 0
}

rm(lc1s)

# Additional variables for repsondents who are nabakyalas
nabas <- c("action_q8_1d_2_1",
					 "action_q8_1d_2_2", 
					 "action_q8_1d_2_3", 
					 "action_q8_1d_2_4")

# Answer 2 is in line with the norms for Nabakyalas
ug2cb$action_q8_1d_2_1$answer[2,]
ug2cb$action_q8_1d_2_2$answer[2,]
ug2cb$action_q8_1d_2_3$answer[2,]
ug2cb$action_q8_1d_2_4$answer[2,]

for(variable in nabas) {
	ug2$involve_nabakyala[ug2[,variable] == 2] <- 1
	ug2$involve_nabakyala[ug2[,variable] == 1] <- 0
}

rm(nabas)

# Absenteeism -------------------------------------------------------------

abs_vars <- c("lc1",
							"pta",
							"bring_up",
							"assemble")

ug2[,abs_vars] <- NA

ug2cb$fire_teach_q10_1_1$answer[2,]
ug2cb$fire_teach_q10_1_2$answer[2,]
ug2cb$fire_teach_q10_1_3$answer[2,]
ug2cb$fire_teach_q10_1_4$answer[2,]
ug2cb$pta_q10_2_1$answer[1,]
ug2cb$pta_q10_2_2$answer[1,]
ug2cb$pta_q10_2_3$answer[1,]
ug2cb$pta_q10_2_4$answer[1,]
ug2cb$bring_up_q10_3_1$answer[2,]
ug2cb$bring_up_q10_3_2$answer[2,]
ug2cb$bring_up_q10_3_3$answer[2,]
ug2cb$bring_up_q10_3_4$answer[2,]
ug2cb$assemble_q10_4_1$answer[1,]
ug2cb$assemble_q10_4_2$answer[1,]
ug2cb$assemble_q10_4_3$answer[1,]
ug2cb$assemble_q10_4_4$answer[1,]

mapply(FUN = get_dummy_ug2,
			 new_var = abs_vars,
			 old_vars = list(c("fire_teach_q10_1_1",
			 									"fire_teach_q10_1_2",
			 									"fire_teach_q10_1_3",
			 									"fire_teach_q10_1_4"),
			 								c("pta_q10_2_1",
			 									"pta_q10_2_2",
			 									"pta_q10_2_3",
			 									"pta_q10_2_4"),
			 								c("bring_up_q10_3_1",
			 									"bring_up_q10_3_2",
			 									"bring_up_q10_3_3",
			 									"bring_up_q10_3_4"),
			 								c("assemble_q10_4_1",
			 									"assemble_q10_4_2",
			 									"assemble_q10_4_3",
			 									"assemble_q10_4_4")),
			 pos_level = c(2,1,2,1),
			 neg_level = c(1,2,1,2))

rm(abs_vars)

# Additional variables for repsondents who are LC1s

lc1s <- c("fire_teach_q10_1_1_2",
					"fire_teach_q10_1_2_2",
					"fire_teach_q10_1_3_2",
					"fire_teach_q10_1_4_2")

ug2cb$fire_teach_q10_1_1_2$answer[2,]
ug2cb$fire_teach_q10_1_2_2$answer[2,]
ug2cb$fire_teach_q10_1_3_2$answer[2,]
ug2cb$fire_teach_q10_1_4_2$answer[2,]

for(variable in lc1s) {
	ug2$lc1[ug2[,variable] == 2] <- 1
	ug2$lc1[ug2[,variable] == 1] <- 0
}

rm(lc1s)


# Abortion ----------------------------------------------------------------


abo_vars <- c("vht",
							"see_doctor",
							"advise_councelling",
							"compassion")

ug2[,abo_vars] <- NA

ug2cb$niece_q12_4a_1$answer[2,]
ug2cb$niece_q12_4a_2$answer[2,]
ug2cb$niece_q12_4b_1$answer[1,]
ug2cb$niece_q12_4b_2$answer[1,]
ug2cb$eight_children_q12_5a_1$answer[2,]
ug2cb$eight_children_q12_5a_2$answer[2,]
ug2cb$eight_children_q12_5b_1$answer[1,]
ug2cb$eight_children_q12_5b_2$answer[1,]

mapply(
	FUN = get_dummy_ug2,
	new_var = abo_vars,
	old_vars = list(c("niece_q12_4a_1",
										"niece_q12_4a_2"),
									c("niece_q12_4b_1",
										"niece_q12_4b_2"),
									c("eight_children_q12_5a_1",
										"eight_children_q12_5a_2"),
									c("eight_children_q12_5b_1",
										"eight_children_q12_5b_2")),
	pos_level = c(2,1,2,1),
	neg_level = c(1,2,1,2))

rm(abo_vars)

# Additional variables for repsondents who are VHTs

ug2cb$niece_q12_4a_1_2$answer[2,]
ug2cb$niece_q12_4a_2_2$answer[2,]

vhts <- c("niece_q12_4a_1_2",
					"niece_q12_4a_2_2")

for(variable in vhts) {
	ug2$vht[ug2[,variable] == 2] <- 1
	ug2$vht[ug2[,variable] == 1] <- 0
}

rm(vhts)

# Additional variables for repsondents who are nabakyalas

ug2cb$eight_children_q12_5a_1_2$answer[2,]
ug2cb$eight_children_q12_5a_2_2$answer[2,]

nabas <- c("eight_children_q12_5a_1_2",
					 "eight_children_q12_5a_2_2")

for(i in 1:length(nabas)) {
	ug2$advise_councelling[ug2[,variable] == 2] <- 1
	ug2$advise_councelling[ug2[,variable] == 1] <- 0
}

rm(nabas)

rm(i,variable,get_dummy_ug2)

